const app = getApp()
const host = app.globalData.host

Page({
  data: {
    inviter: 0,
    code: null,
    users:"",
    loadState: false,   
    logoSrc: 'https://yuanjia-db.oss-cn-beijing.aliyuncs.com/%E7%A7%81%E4%BA%BA%E5%BE%8B%E5%B8%88%E5%B0%8F%E7%A8%8B%E5%BA%8F/accredit-logo.jpg',
    iconSrc: 'https://yuanjia-db.oss-cn-beijing.aliyuncs.com/%E7%A7%81%E4%BA%BA%E5%BE%8B%E5%B8%88%E5%B0%8F%E7%A8%8B%E5%BA%8F/accredit-icon.png',
  },
  onLoad: function (options) {
    var that = this

    // console.log('options参数：' + options.inviter)
    var inviter = options.inviter!==undefined ? options.inviter : 0
    that.setData({
      inviter: inviter
    })

    // 登录
    that.loginFn()
  },
  
  getUserInfo: function (res) {
    var that = this;
    var userRes = JSON.parse(res.detail.rawData);
    console.log(res);
    that.setData({
      loadState: true
    })
    wx.request({
      url: host+'/users/sessions',
      method: 'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        code: that.data.code
      },
      success: function (reponse) {
        console.log(reponse);
        that.setData({
          loadState: true
        })
        if (reponse.data.code == 0){
          var token = reponse.data.data.token;
          if (reponse.data.data.flag == 0){
            wx.switchTab({
              url: '/pages/danger/index'
            })
          }else{
            wx.request({
              url: host + '/users/sessions',
              method: 'POST',
              header: {
                'content-type': 'application/json'
              },
              data: {
                encrypted: res.detail.encryptedData,
                nickName: userRes.nickName,
                gender: userRes.gender,
                language: userRes.language,
                city: userRes.city,
                province: userRes.province,
                country: userRes.country,
                avatarUrl: userRes.avatarUrl,
                token: token,
                iv:res.detail.iv
              },
              success: function (res) {
                wx.setStorageSync('token',token);
                // 登录成功，跳转到主页
                if(res.data.code == 0){
                  wx.redirectTo({
                    url: '/pages/getphone/getphone'
                  })
                }else {
                  app.globalData.isShow(res.data.msg)
                }
                
              },
              fail: function (res) {
                console.log(res)
                wx.showToast({
                  title: '登录失败！' + res.data.msg,
                  icon: 'none',
                  duration: 2000
                })
              },
              complete: function () {
                that.setData({
                  loadState: false
                })
              }
            })
          }
        } else {
          app.globalData.isShow(res.data.msg)
        }
          // 获取到用户详情回传后台
      },
      fail: function (res) {
        console.log(res)
        wx.showToast({
          title: '登录失败！' + res.data.msg,
          icon: 'none',
          duration: 2000
        })
      },
      complete: function () {
        that.setData({
          loadState: false
        })
      }
    })
  },
  
  loginFn: function () {
    var that = this
    wx.login({
      success: function (res) {
        var logRes = res
        if (res.code) {
          // 传code给后台，后台返回用户ID
          console.log("code值："+res.code);
          that.setData({
            code: res.code
          })
          wx.setStorageSync("code", res.code);
        } else {
          // console.log('登录失败！' + res.errMsg)
          wx.showToast({
            title: '登录失败！' + res.errMsg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    });
  }
})